<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function execCode(counter) {
      const promise = new Promise((resolve, reject) => {
        setTimeout(function () {
          if (counter > 0) {
            let total = 0
            for (let i = 0; i < counter; i++) {
              total += i
            }
            // 成功的回调
            resolve(total)
          } else {
            // 失败的回调
            reject(`${counter}值有问题`)
          }
        }, 3000);
      })
      return promise
    }

    const promise = execCode(100)

    promise.then((value) => {
      console.log('成功有了结果:', value)
    })

    promise.catch((err) => {
      console.log('失败有了错误:', err)
    })

    const promise2 = execCode(-2)
    promise2.then((value) => {
      console.log('成功有了结果:', value)
    })
    promise2.catch((err) => {
      console.log('失败有了错误:', err)
    })

    execCode(255).then((value) => {
      console.log('成功有了结果:', value)
    }).catch((err) => {
      console.log('失败有了错误:', err)
    })

  </script>
</body>

</html>